// Registry.cpp: implementation of the CRegistry class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ansieditor.h"
#include "Registry.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRegistry::CRegistry()
{

}

CRegistry::~CRegistry()
{

}

BOOL CRegistry::QueryValue(HKEY hKey, LPCTSTR szSubKey, LPCTSTR szValueName, BYTE **ppBuf, DWORD *dwSize)
{
	HKEY hSubKey = 0;
	BOOL bResult = FALSE;
	if(::RegOpenKeyEx(hKey, szSubKey, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS)
	{
		if(::RegQueryValueEx(hSubKey, szValueName, NULL, NULL, NULL, dwSize) == ERROR_SUCCESS)
		{
			*ppBuf = new BYTE[(int)dwSize];
			if(::RegQueryValueEx(hSubKey, szValueName, NULL, NULL, *ppBuf, dwSize) == ERROR_SUCCESS)
			{
				bResult = TRUE;
			}
		}
		::RegCloseKey(hSubKey);
	}

	return bResult;
}

BOOL CRegistry::SetValue(HKEY hKey, LPCTSTR szSubKey, LPCTSTR szValueName, DWORD dwType, void *pBuf, DWORD dwSize /* = -1 */)
{
	HKEY hSubKey;

	LONG lResult = ::RegCreateKeyEx(hKey, szSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL);
	if(lResult != ERROR_SUCCESS)
	{
		return FALSE;
	}

	BOOL bResult = TRUE;
	if(::RegSetValueEx(hSubKey, szValueName, 0, dwType, (BYTE*)pBuf, dwSize) != ERROR_SUCCESS)
	{
		bResult = FALSE;
	}
	::RegCloseKey(hSubKey);
	return bResult;
}